<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>《Dart语言基础之设计模式》 - Lucklyの博客 - 在阅读中遇见自己</title><meta name="Description" content="早起的年轻人做的网站"><meta property="og:title" content="《Dart语言基础之设计模式》" />
<meta property="og:description" content="
本文主要介绍
" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" />
<meta property="og:image" content="https://luckly.work/logo.png"/>
<meta property="article:published_time" content="2021-07-28T19:39:10+08:00" />
<meta property="article:modified_time" content="2021-07-28T19:39:10+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://luckly.work/logo.png"/>

<meta name="twitter:title" content="《Dart语言基础之设计模式》"/>
<meta name="twitter:description" content="
本文主要介绍
"/>
<meta name="application-name" content="LoveIt">
<meta name="apple-mobile-web-app-title" content="LoveIt"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" /><link rel="prev" href="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bstring/" /><link rel="next" href="https://luckly.work/flutter%E4%B8%ADshapeborder%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93/" /><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.7.2/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "《Dart语言基础之设计模式》",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/luckly.work\/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F\/"
        },"image": [{
                            "@type": "ImageObject",
                            "url": "https:\/\/luckly.work\/images\/Apple-Devices-Preview.png",
                            "width":  3200 ,
                            "height":  2048 
                        }],"genre": "posts","keywords": "flutter","wordcount":  9177 ,
        "url": "https:\/\/luckly.work\/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F\/","datePublished": "2021-07-28T19:39:10+08:00","dateModified": "2021-07-28T19:39:10+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "https:\/\/luckly.work\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "早起的年轻人"
            },"description": ""
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><div class="logo-wrapper">
  <a href="/%20/" class="logo">Lucklyの博客 - 在阅读中遇见自己</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
      <a class="menu-item-link" href="/posts/">所有文章</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/flutter/">Flutter</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/read/">读书笔记</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/go/">Go</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/tags/">标签</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/categories/">分类</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/about/">关于</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="https://github.com/ITmxs">Github</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/friend/">友链</a>
    </li>
  </ul>
</nav><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">《Dart语言基础之设计模式》</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>早起的年轻人</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/flutter/"><i class="far fa-folder fa-fw"></i>Flutter</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2021-07-28">2021-07-28</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 9177 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 19 分钟&nbsp;</div>
        </div><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        data-srcset="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 1.5x, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 2x"
        data-sizes="auto"
        alt="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        title="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png" /></div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#设计模式">设计模式</a>
      <ul>
        <li><a href="#六大原则">六大原则</a>
          <ul>
            <li><a href="#开闭原则open-closed-principle">开闭原则（Open-Closed-Principle）</a></li>
            <li><a href="#里氏代换原则liskow-substitution-principle">里氏代换原则（Liskow-Substitution-Principle）</a></li>
            <li><a href="#依赖倒转原则dependency-inversion-principle">依赖倒转原则（Dependency-Inversion-Principle）</a></li>
            <li><a href="#单一职责原则single-responsibility-principle">单一职责原则（Single-Responsibility-Principle）</a></li>
            <li><a href="#接口隔离原则interface-segregation-principle">接口隔离原则（Interface-Segregation-Principle）</a></li>
            <li><a href="#迪米特法则law-of-demeter">迪米特法则（Law-Of-Demeter）</a></li>
          </ul>
        </li>
        <li><a href="#工厂模式">工厂模式</a>
          <ul>
            <li><a href="#模式分析">模式分析</a></li>
            <li><a href="#模式难点">模式难点</a></li>
            <li><a href="#模式解决问题">模式解决问题</a></li>
            <li><a href="#优点">优点</a></li>
            <li><a href="#缺点">缺点</a></li>
            <li><a href="#模式应用场景">模式应用场景</a></li>
            <li><a href="#模式代码">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#单例模式">单例模式</a>
          <ul>
            <li><a href="#模式分析-1">模式分析</a></li>
            <li><a href="#模式解决问题-1">模式解决问题</a></li>
            <li><a href="#优点-1">优点</a></li>
            <li><a href="#缺点-1">缺点</a></li>
            <li><a href="#模式应用场景-1">模式应用场景</a></li>
            <li><a href="#模式代码-1">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#建造者模式">建造者模式</a>
          <ul>
            <li><a href="#模式分析-2">模式分析</a></li>
            <li><a href="#模式难点-1">模式难点</a></li>
            <li><a href="#模式解决问题-2">模式解决问题</a></li>
            <li><a href="#优点-2">优点</a></li>
            <li><a href="#缺点-2">缺点</a></li>
            <li><a href="#模式应用场景-2">模式应用场景</a></li>
            <li><a href="#模式代码-2">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#原型模式">原型模式</a>
          <ul>
            <li><a href="#模式分析-3">模式分析</a></li>
            <li><a href="#模式解决问题-3">模式解决问题</a></li>
            <li><a href="#模式难点-2">模式难点</a></li>
            <li><a href="#优点-3">优点</a></li>
            <li><a href="#缺点-3">缺点</a></li>
            <li><a href="#模式应用场景-3">模式应用场景</a></li>
            <li><a href="#模式代码-3">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#适配器模式">适配器模式</a>
          <ul>
            <li><a href="#模式分析-4">模式分析</a></li>
            <li><a href="#模式难点-3">模式难点</a></li>
            <li><a href="#模式解决问题-4">模式解决问题</a></li>
            <li><a href="#优点-4">优点</a></li>
            <li><a href="#缺点-4">缺点</a></li>
            <li><a href="#模式应用场景-4">模式应用场景</a></li>
            <li><a href="#模式代码-4">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#桥接模式bridge">桥接模式（Bridge)</a>
          <ul>
            <li><a href="#模式分析-5">模式分析</a></li>
            <li><a href="#模式难点-4">模式难点</a></li>
            <li><a href="#模式解决问题-5">模式解决问题</a></li>
            <li><a href="#优点-5">优点</a></li>
            <li><a href="#缺点-5">缺点</a></li>
            <li><a href="#模式应用场景-5">模式应用场景</a></li>
            <li><a href="#模式代码-5">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#装饰器模式decorator-pattern">装饰器模式（Decorator Pattern）</a>
          <ul>
            <li><a href="#模式分析-6">模式分析</a></li>
            <li><a href="#模式难点-5">模式难点</a></li>
            <li><a href="#模式解决问题-6">模式解决问题</a></li>
            <li><a href="#优点-6">优点</a></li>
            <li><a href="#缺点-6">缺点</a></li>
            <li><a href="#模式应用场景-6">模式应用场景</a></li>
            <li><a href="#模式代码-6">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#外观模式facade">外观模式(Facade)</a>
          <ul>
            <li><a href="#模式分析-7">模式分析</a></li>
            <li><a href="#模式难点-6">模式难点</a></li>
            <li><a href="#模式解决问题-7">模式解决问题</a></li>
            <li><a href="#优点-7">优点</a></li>
            <li><a href="#缺点-7">缺点</a></li>
            <li><a href="#模式应用场景-7">模式应用场景</a></li>
            <li><a href="#模式代码-7">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#享元模式flyweight">享元模式(Flyweight)</a>
          <ul>
            <li><a href="#模式分析-8">模式分析</a></li>
            <li><a href="#模式难点-7">模式难点</a></li>
            <li><a href="#模式解决问题-8">模式解决问题</a></li>
            <li><a href="#优点-8">优点</a></li>
            <li><a href="#缺点-8">缺点</a></li>
            <li><a href="#模式应用场景-8">模式应用场景</a></li>
            <li><a href="#模式代码-8">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#代理模式">代理模式</a>
          <ul>
            <li><a href="#模式分析-9">模式分析</a></li>
            <li><a href="#模式难点-8">模式难点</a></li>
            <li><a href="#模式解决问题-9">模式解决问题</a></li>
            <li><a href="#优点-9">优点</a></li>
            <li><a href="#缺点-9">缺点</a></li>
            <li><a href="#模式应用场景-9">模式应用场景</a></li>
            <li><a href="#模式代码-9">模式代码</a></li>
          </ul>
        </li>
        <li><a href="#发布订阅模式">发布订阅模式</a>
          <ul>
            <li><a href="#模式分析-10">模式分析</a></li>
            <li><a href="#模式难点-9">模式难点</a></li>
            <li><a href="#模式解决问题-10">模式解决问题</a></li>
            <li><a href="#优点-10">优点</a></li>
            <li><a href="#缺点-10">缺点</a></li>
            <li><a href="#模式应用场景-10">模式应用场景</a></li>
            <li><a href="#模式代码-10">模式代码</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><blockquote>
<p>本文主要介绍</p>
</blockquote>
<h1 id="设计模式">设计模式</h1>
<p><a href="https://www.runoob.com/design-pattern/" target="_blank" rel="noopener noreffer">参考</a> 模式分析，模式难点，模式解决问题，优点，缺点，模式应用场景，模式代码（基于dart)</p>
<h2 id="六大原则">六大原则</h2>
<h3 id="开闭原则open-closed-principle">开闭原则（Open-Closed-Principle）</h3>
<p>核心：一个软件实体应当对拓展开放，对修改关闭。即：软件实体应尽量在不修改原有代码的情况下进行拓展。</p>
<h3 id="里氏代换原则liskow-substitution-principle">里氏代换原则（Liskow-Substitution-Principle）</h3>
<p>核心：所有引用基类（父类）的地方，都必须能透明地使用其子类的对象。</p>
<p>一个软件实体如果使用的是一个父类，那么一定适用于其子类，而且他察觉不出父类对象和子类对象的区别。也就是说，在软件里面，把父类都替换成它的子类，程序的行为没有变化。简单地说，子类必须能够替换掉它们的父类型。</p>
<h3 id="依赖倒转原则dependency-inversion-principle">依赖倒转原则（Dependency-Inversion-Principle）</h3>
<p>核心: 抽象不应该依赖于细节，细节应当依赖于抽象。换言之，要针对接口编程，而非针对实现编程。</p>
<p>思想: 抽象不应该依赖于细节，细节应当依赖于抽象。换言之，要针对接口编程，而不是针对实现编程。依赖倒转其实可以说是面向对象设计的标志，用哪种语言来编写程序不重要，如果编写时考虑的都是如何针对抽象编程而不是针对细节编程，即 程序中所有的依赖关系都是终止于抽象类或者接口，那就是面向对象的设计，反之就是面向过程化设计了。</p>
<p>原则：</p>
<ol>
<li>高层模块不应该依赖于低层模块。两个都应该依赖于抽象。</li>
<li>抽象不应该依赖细节。细节应该依赖于抽象。</li>
</ol>
<h3 id="单一职责原则single-responsibility-principle">单一职责原则（Single-Responsibility-Principle）</h3>
<p>核心：一个类只负责一个功能领域中相应的职责，或者可以定义为：就一个类而言，应该只有一个引起它变化的原因。</p>
<p>思想：如果一个类承担的职责过多，就等于把这些职责耦合在一起，一个职责的变化可能会削弱或者抑制这个类完成其他职责的能力。这种耦合会导致脆弱的设计，当变化发生时，设计会遭受到意想不到的破坏。【Eg:游戏的界面组成与逻辑组成分离】</p>
<h3 id="接口隔离原则interface-segregation-principle">接口隔离原则（Interface-Segregation-Principle）</h3>
<p>核心：使用多个专门的接口，而不使用单一的总接口。即 客户端不应该依赖于那些它不需要的接口。</p>
<h3 id="迪米特法则law-of-demeter">迪米特法则（Law-Of-Demeter）</h3>
<p>核心：一个软件实体应当尽可能少地与其他实体发生作用。（无熟人难办事）</p>
<p>思想：也叫最少知识原则。如果两个类不彼此通信，那么这两个类就不应当直接地发生相互作用。如果其中一个类需要另一个类的某一个方法的话，可以通过第三者转发这个调用。（不要和陌生人说话）</p>
<p>原则： 在迪米特法则中，对于一个对象，其朋友包括如下几类：</p>
<ol>
<li>当前对象 this</li>
<li>以参数形式传入到当前对象方法中的对象</li>
<li>当前对象的成员对象</li>
<li>若当前对象的成员你对象是一个集合，那么集合中的对象也都是朋友</li>
<li>当前对象所创建的对象</li>
</ol>
<h2 id="工厂模式">工厂模式</h2>
<h3 id="模式分析">模式分析</h3>
<p>定义一个创建对象的接口，让其子类自己决定实例化哪一个工厂类，工厂模式使其创建过程延迟到子类进行</p>
<h3 id="模式难点">模式难点</h3>
<h3 id="模式解决问题">模式解决问题</h3>
<p>该模式主要解决接口选择的问题。我们明确地计划不同条件下创建不同实例时，让其子类实现工厂接口，返回的也是一个抽象的产品</p>
<h3 id="优点">优点</h3>
<ol>
<li>一个调用者想创建一个对象，只要知道其名称就可以了。</li>
<li>扩展性高，如果想增加一个产品，只要扩展一个工厂类就可以。</li>
<li>屏蔽产品的具体实现，调用者只关心产品的接口。</li>
</ol>
<h3 id="缺点">缺点</h3>
<p>每次增加一个产品时，都需要增加一个具体类和对象实现工厂，使得系统中类的个数成倍增加，在一定程度上增加了系统的复杂度，同时也增加了系统具体类的依赖。</p>
<h3 id="模式应用场景">模式应用场景</h3>
<ol>
<li>日志记录器：记录可能记录到本地硬盘、系统事件、远程服务器等，用户可以选择记录日志到什么地方。</li>
<li>数据库访问，当用户不知道最后系统采用哪一类数据库，以及数据库可能有变化时。</li>
<li>设计一个连接服务器的框架，需要三个协议，&ldquo;POP3&rdquo;、&ldquo;IMAP&rdquo;、&ldquo;HTTP&rdquo;，可以把这三个作为产品类，共同实现一个接口。</li>
</ol>
<h3 id="模式代码">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Pen</span> <span class="p">{</span>
  <span class="n">draw</span><span class="p">();</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">Pencil</span> <span class="kd">implements</span> <span class="n">Pen</span> <span class="p">{</span>
  <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;this is Pencil&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">ColorPen</span> <span class="kd">implements</span> <span class="n">Pen</span> <span class="p">{</span>
  <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;this is ColorPen&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>
<span class="kd">class</span> <span class="nc">RunFactory</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="n">pencil</span> <span class="o">=</span> <span class="n">getPen</span><span class="p">(</span><span class="s2">&#34;Pencil&#34;</span><span class="p">);</span>
    <span class="kd">var</span> <span class="n">colorPen</span> <span class="o">=</span> <span class="n">getPen</span><span class="p">(</span><span class="s2">&#34;ColorPen&#34;</span><span class="p">);</span>
    <span class="n">pencil</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>
    <span class="n">colorPen</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>
  <span class="p">}</span>
  <span class="n">Pen</span> <span class="n">getPen</span><span class="p">(</span><span class="kt">String</span> <span class="n">penStr</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">penStr</span> <span class="o">==</span> <span class="s2">&#34;Pencil&#34;</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">return</span> <span class="k">new</span> <span class="n">Pencil</span><span class="p">();</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">penStr</span> <span class="o">==</span> <span class="s2">&#34;ColorPen&#34;</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">return</span> <span class="k">new</span> <span class="n">ColorPen</span><span class="p">();</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
      <span class="k">return</span> <span class="kc">null</span><span class="p">;</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="单例模式">单例模式</h2>
<blockquote>
<p>这种类型的设计模式属于创建型模式，它提供了一种创建对象的最佳方式。</p>
</blockquote>
<blockquote>
<p>这种模式涉及到一个单一的类，该类负责创建自己的对象，同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式，可以直接访问，不需要实例化该类的对象。</p>
</blockquote>
<h3 id="模式分析-1">模式分析</h3>
<ol>
<li>单例类只能有一个实例。</li>
<li>单例类必须自己创建自己的唯一实例。</li>
<li>单例类必须给所有其他对象提供这一实例。</li>
</ol>
<h3 id="模式解决问题-1">模式解决问题</h3>
<ol>
<li>控制实例数目，节省系统资源。</li>
<li>保证该类只有一个实例，多处共享</li>
</ol>
<h3 id="优点-1">优点</h3>
<ol>
<li>内存中只有一个实例，减少了内存开销，尤其是频繁创建和销毁实例</li>
<li>避免对资源的多重占用</li>
<li>保证全局唯一实例从而保证实例状态一致</li>
</ol>
<h3 id="缺点-1">缺点</h3>
<ol>
<li>没有接口，不能继承</li>
<li>大量使用也会导致内存泄漏</li>
</ol>
<h3 id="模式应用场景-1">模式应用场景</h3>
<ol>
<li>要求生产唯一序列号。</li>
<li>WEB 中的计数器，不用每次刷新都在数据库里加一次，用单例先缓存起来。</li>
<li>创建的一个对象需要消耗的资源过多，比如 I/O 与数据库的连接等。</li>
</ol>
<h3 id="模式代码-1">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="kd">class</span> <span class="nc">_SingleTon</span> <span class="p">{</span>
  <span class="n">_SingleTon</span><span class="p">.</span><span class="n">_</span><span class="p">();</span>
  <span class="c1">//第一种方式调用
</span><span class="c1"></span>  <span class="kd">factory</span> <span class="n">_SingleTon</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">instance</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="c1">//第二种方式调用
</span><span class="c1"></span>  <span class="kd">static</span> <span class="n">_SingleTon</span> <span class="n">instance</span> <span class="o">=</span> <span class="n">_SingleTon</span><span class="p">.</span><span class="n">_</span><span class="p">();</span>
  <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="m">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">RunSingleTon</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s1">&#39;单例模式创建&#39;</span><span class="p">);</span>
    <span class="n">print</span><span class="p">(</span><span class="n">_SingleTon</span><span class="p">().</span><span class="n">count</span><span class="o">++</span><span class="p">);</span>
    <span class="n">print</span><span class="p">(</span><span class="n">_SingleTon</span><span class="p">.</span><span class="n">instance</span><span class="p">.</span><span class="n">count</span><span class="o">++</span><span class="p">);</span>
    <span class="n">print</span><span class="p">(</span><span class="n">_SingleTon</span><span class="p">.</span><span class="n">instance</span><span class="p">.</span><span class="n">count</span><span class="o">++</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="建造者模式">建造者模式</h2>
<h3 id="模式分析-2">模式分析</h3>
<p>建造者模式（Builder Pattern）使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式，它提供了一种创建对象的最佳方式。</p>
<p>一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。</p>
<h3 id="模式难点-1">模式难点</h3>
<p>如何抽象出不同特性需要很强的抽象思维</p>
<h3 id="模式解决问题-2">模式解决问题</h3>
<p>主要解决在软件系统中，有时候面临着&quot;一个复杂对象&quot;的创建工作，其通常由各个部分的子对象用一定的算法构成；由于需求的变化，这个复杂对象的各个部分经常面临着剧烈的变化，但是将它们组合在一起的算法却相对稳定。</p>
<h3 id="优点-2">优点</h3>
<ol>
<li>建造者独立，易扩展。</li>
<li>便于控制细节风险。</li>
</ol>
<h3 id="缺点-2">缺点</h3>
<ol>
<li>产品必须有共同点，范围有限制。</li>
<li>如内部变化复杂，会有很多的建造类。</li>
</ol>
<h3 id="模式应用场景-2">模式应用场景</h3>
<ol>
<li>需要生成的对象具有复杂的内部结构。</li>
<li>需要生成的对象内部属性本身相互依赖。</li>
</ol>
<h3 id="模式代码-2">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span><span class="lnt">106
</span><span class="lnt">107
</span><span class="lnt">108
</span><span class="lnt">109
</span><span class="lnt">110
</span><span class="lnt">111
</span><span class="lnt">112
</span><span class="lnt">113
</span><span class="lnt">114
</span><span class="lnt">115
</span><span class="lnt">116
</span><span class="lnt">117
</span><span class="lnt">118
</span><span class="lnt">119
</span><span class="lnt">120
</span><span class="lnt">121
</span><span class="lnt">122
</span><span class="lnt">123
</span><span class="lnt">124
</span><span class="lnt">125
</span><span class="lnt">126
</span><span class="lnt">127
</span><span class="lnt">128
</span><span class="lnt">129
</span><span class="lnt">130
</span><span class="lnt">131
</span><span class="lnt">132
</span><span class="lnt">133
</span><span class="lnt">134
</span><span class="lnt">135
</span><span class="lnt">136
</span><span class="lnt">137
</span><span class="lnt">138
</span><span class="lnt">139
</span><span class="lnt">140
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="c1">//建造者模式
</span><span class="c1"></span>
<span class="k">import</span> <span class="s1">&#39;run.dart&#39;</span><span class="p">;</span>

<span class="c1">//建造者模式的精髓在于抽象，要把内容都抽象成一个一个的类来实现
</span><span class="c1">//标识食物的接口
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Item</span> <span class="p">{</span>
  <span class="kt">String</span> <span class="n">name</span><span class="p">;</span>
  <span class="n">Packing</span> <span class="n">packing</span><span class="p">();</span>
  <span class="kt">double</span> <span class="n">price</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">//包装接口
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Packing</span> <span class="p">{</span>
  <span class="kt">String</span> <span class="n">pack</span><span class="p">();</span>
<span class="p">}</span>

<span class="c1">//实现包装接口的实体类
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">Wrapper</span> <span class="kd">implements</span> <span class="n">Packing</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="n">pack</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="s2">&#34;Wrapper&#34;</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">Bottle</span> <span class="kd">implements</span> <span class="n">Packing</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">pack</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="s2">&#34;Bottle&#34;</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="c1">///创建实现Item接口的抽象类
</span><span class="c1">//使用包装纸包装的食物
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Burger</span> <span class="kd">implements</span> <span class="n">Item</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="n">Packing</span> <span class="n">packing</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="k">new</span> <span class="n">Wrapper</span><span class="p">();</span>
  <span class="p">}</span>
<span class="p">}</span>
<span class="c1">//使用瓶子包装的食物
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">ColdDrink</span> <span class="kd">implements</span> <span class="n">Item</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="n">Packing</span> <span class="n">packing</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="k">new</span> <span class="n">Bottle</span><span class="p">();</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="c1">//创建扩展了Burger和ColdDrink的实体类
</span><span class="c1">//蔬菜
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">VegBurger</span> <span class="kd">extends</span> <span class="n">Burger</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;Veg Burger&#34;</span><span class="p">;</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">double</span> <span class="n">price</span> <span class="o">=</span> <span class="m">34.3</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">//鸡肉汉堡包
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">ChickenBurger</span> <span class="kd">extends</span> <span class="n">Burger</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;Chicken Burger&#34;</span><span class="p">;</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">double</span> <span class="n">price</span> <span class="o">=</span> <span class="m">56.2</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">//可乐
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">Coke</span> <span class="kd">extends</span> <span class="n">ColdDrink</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;Coke&#34;</span><span class="p">;</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">double</span> <span class="n">price</span> <span class="o">=</span> <span class="m">3</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">//百事
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">Pepsi</span> <span class="kd">extends</span> <span class="n">ColdDrink</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;Pepsi&#34;</span><span class="p">;</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">double</span> <span class="n">price</span> <span class="o">=</span> <span class="m">4</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">//创建Meal类用于打印实例化的对象信息
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">Meal</span> <span class="p">{</span>
  <span class="n">List</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;</span> <span class="n">_items</span> <span class="o">=</span> <span class="p">[];</span>
  <span class="kt">void</span> <span class="n">addItem</span><span class="p">(</span><span class="n">Item</span> <span class="n">item</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="n">_items</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
  <span class="c1">//计算所有项目的总价
</span><span class="c1"></span>  <span class="kt">double</span> <span class="n">getCost</span><span class="p">()</span> <span class="p">{</span>
    <span class="kt">double</span> <span class="n">cost</span> <span class="o">=</span> <span class="m">0</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="n">item</span> <span class="k">in</span> <span class="n">_items</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">cost</span> <span class="o">+=</span> <span class="n">item</span><span class="p">.</span><span class="n">price</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">cost</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="c1">//展示当前项目
</span><span class="c1"></span>  <span class="n">showItems</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="n">item</span> <span class="k">in</span> <span class="n">_items</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">print</span><span class="p">(</span><span class="s2">&#34;item:&#34;</span> <span class="o">+</span>
          <span class="n">item</span><span class="p">.</span><span class="n">name</span> <span class="o">+</span>
          <span class="s2">&#34;, Packing: &#34;</span> <span class="o">+</span>
          <span class="n">item</span><span class="p">.</span><span class="n">packing</span><span class="p">().</span><span class="n">pack</span><span class="p">()</span> <span class="o">+</span>
          <span class="s2">&#34;, Price:&#34;</span> <span class="o">+</span>
          <span class="n">item</span><span class="p">.</span><span class="n">price</span><span class="p">.</span><span class="n">toString</span><span class="p">());</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">MealBuilder</span> <span class="p">{</span>
  <span class="n">Meal</span> <span class="n">prepareVegMeal</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">Meal</span> <span class="n">meal</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Meal</span><span class="p">();</span>
    <span class="n">meal</span><span class="p">.</span><span class="n">addItem</span><span class="p">(</span><span class="k">new</span> <span class="n">VegBurger</span><span class="p">());</span>
    <span class="n">meal</span><span class="p">.</span><span class="n">addItem</span><span class="p">(</span><span class="k">new</span> <span class="n">Coke</span><span class="p">());</span>
    <span class="k">return</span> <span class="n">meal</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="n">Meal</span> <span class="n">prepareNonVegMeal</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">Meal</span> <span class="n">meal</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Meal</span><span class="p">();</span>
    <span class="n">meal</span><span class="p">.</span><span class="n">addItem</span><span class="p">(</span><span class="k">new</span> <span class="n">ChickenBurger</span><span class="p">());</span>
    <span class="n">meal</span><span class="p">.</span><span class="n">addItem</span><span class="p">(</span><span class="k">new</span> <span class="n">Pepsi</span><span class="p">());</span>
    <span class="k">return</span> <span class="n">meal</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">RunBuilderPattern</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s1">&#39;建造者模式创建&#39;</span><span class="p">);</span>
    <span class="n">MealBuilder</span> <span class="n">mealBuilder</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MealBuilder</span><span class="p">();</span>
    <span class="n">Meal</span> <span class="n">vegMeal</span> <span class="o">=</span> <span class="n">mealBuilder</span><span class="p">.</span><span class="n">prepareVegMeal</span><span class="p">();</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Veg Meal&#34;</span><span class="p">);</span>
    <span class="n">vegMeal</span><span class="p">.</span><span class="n">showItems</span><span class="p">();</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Total Cost: &#34;</span> <span class="o">+</span> <span class="n">vegMeal</span><span class="p">.</span><span class="n">getCost</span><span class="p">().</span><span class="n">toString</span><span class="p">());</span>

    <span class="n">Meal</span> <span class="n">nonVegMeal</span> <span class="o">=</span> <span class="n">mealBuilder</span><span class="p">.</span><span class="n">prepareNonVegMeal</span><span class="p">();</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n\n</span><span class="s2">Non-Veg Meal&#34;</span><span class="p">);</span>
    <span class="n">nonVegMeal</span><span class="p">.</span><span class="n">showItems</span><span class="p">();</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Total Cost: &#34;</span> <span class="o">+</span> <span class="n">nonVegMeal</span><span class="p">.</span><span class="n">getCost</span><span class="p">().</span><span class="n">toString</span><span class="p">());</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="原型模式">原型模式</h2>
<h3 id="模式分析-3">模式分析</h3>
<p>原型模式（Prototype Pattern）是用于创建<code>重复的对象</code>，同时又能保证性能。这种类型的设计模式属于<code>创建型模式</code>，它提供了一种创建对象的最佳方式。</p>
<p>这种模式是实现了一个原型接口，该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时，则采用这种模式。例如，一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象，在下一个请求时返回它的克隆，在需要的时候更新数据库，以此来减少数据库调用。</p>
<h3 id="模式解决问题-3">模式解决问题</h3>
<ol>
<li>当一个系统应该独立于它的产品创建，构成和表示时。</li>
<li>当要实例化的类是在运行时刻指定时，例如，通过动态装载。</li>
<li>为了避免创建一个与产品类层次平行的工厂类层次时。</li>
<li>当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些。</li>
</ol>
<h3 id="模式难点-2">模式难点</h3>
<p>克隆</p>
<h3 id="优点-3">优点</h3>
<ol>
<li>性能提高。</li>
<li>逃避构造函数的约束。</li>
</ol>
<h3 id="缺点-3">缺点</h3>
<ol>
<li>配备克隆方法需要对类的功能进行通盘考虑，这对于全新的类不是很难，但对于已有的类不一定很容易，特别当一个类引用不支持串行化的间接对象，或者引用含有循环结构的时候。</li>
<li>必须实现 Cloneable 接口。</li>
</ol>
<h3 id="模式应用场景-3">模式应用场景</h3>
<ol>
<li>资源优化场景。</li>
<li>类初始化需要消化非常多的资源，这个资源包括数据、硬件资源等。</li>
<li>性能和安全要求的场景。</li>
<li>通过 new 产生一个对象需要非常繁琐的数据准备或访问权限，则可以使用原型模式。</li>
<li>一个对象多个修改者的场景。</li>
<li>一个对象需要提供给其他对象访问，而且各个调用者可能都需要修改其值时，可以考虑使用原型模式拷贝多个对象供调用者使用。</li>
<li>在实际项目中，原型模式很少单独出现，一般是和工厂方法模式一起出现，通过 clone 的方法创建一个对象，然后由工厂方法提供给调用者。原型模式已经与 Java 融为浑然一体，大家可以随手拿来使用。</li>
</ol>
<h3 id="模式代码-3">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">ProtoType</span><span class="p">{</span>
  <span class="kt">String</span> <span class="n">_id</span><span class="p">;</span>
  <span class="n">ProtoType</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="n">_id</span><span class="p">);</span>
  <span class="kt">String</span> <span class="kd">get</span> <span class="n">id</span><span class="o">=&gt;</span><span class="n">_id</span><span class="p">;</span>
  <span class="kt">void</span> <span class="kd">set</span> <span class="n">id</span><span class="p">(</span><span class="kt">String</span> <span class="n">id</span><span class="p">)</span><span class="o">=&gt;</span><span class="k">this</span><span class="p">.</span><span class="n">_id</span><span class="o">=</span><span class="n">id</span><span class="p">;</span>
  <span class="n">ProtoType</span> <span class="n">clone</span><span class="p">();</span>
<span class="p">}</span>
<span class="kd">class</span> <span class="nc">ConcretePrototype</span> <span class="kd">extends</span> <span class="n">ProtoType</span><span class="p">{</span>
  <span class="n">ConcretePrototype</span><span class="p">(</span><span class="kt">String</span> <span class="n">id</span><span class="p">,</span><span class="k">this</span><span class="p">.</span><span class="n">name</span><span class="p">)</span> <span class="o">:</span> <span class="k">super</span><span class="p">(</span><span class="n">id</span><span class="p">);</span>
  <span class="kd">final</span> <span class="kt">String</span> <span class="n">name</span><span class="p">;</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="n">ConcretePrototype</span> <span class="n">clone</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">ProtoType</span> <span class="n">protoType</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ConcretePrototype</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="n">id</span><span class="p">,</span><span class="k">this</span><span class="p">.</span><span class="n">name</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">protoType</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>
<span class="kd">class</span> <span class="nc">RunPrototype</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="n">main</span><span class="p">(){</span>
    <span class="kd">var</span> <span class="n">a</span><span class="o">=</span> <span class="n">ConcretePrototype</span><span class="p">(</span><span class="s2">&#34;1&#34;</span><span class="p">,</span><span class="s2">&#34;protoType模式&#34;</span><span class="p">);</span>
    <span class="kd">var</span> <span class="n">b</span><span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="n">clone</span><span class="p">();</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;实例a的hashCode:&#34;</span><span class="o">+</span><span class="n">a</span><span class="p">.</span><span class="n">hashCode</span><span class="p">.</span><span class="n">toString</span><span class="p">());</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;实例b的hashCode:&#34;</span><span class="o">+</span><span class="n">b</span><span class="p">.</span><span class="n">hashCode</span><span class="p">.</span><span class="n">toString</span><span class="p">());</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;实例a的name:&#34;</span><span class="o">+</span><span class="n">a</span><span class="p">.</span><span class="n">name</span><span class="p">);</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;实例b的name:&#34;</span><span class="o">+</span><span class="n">b</span><span class="p">.</span><span class="n">name</span><span class="p">);</span>
  <span class="p">}</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span><span class="o">=</span><span class="s2">&#34;原型模式&#34;</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="适配器模式">适配器模式</h2>
<h3 id="模式分析-4">模式分析</h3>
<p>适配器模式（Adapter Pattern）是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式，它结合了两个独立接口的功能。 这种模式涉及到一个单一的类，该类负责加入独立的或不兼容的接口功能。举个真实的例子，读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入读卡器，再将读卡器插入笔记本，这样就可以通过笔记本来读取内存卡。 我们通过下面的实例来演示适配器模式的使用。其中，音频播放器设备只能播放 mp3 文件，通过使用一个更高级的音频播放器来播放 vlc 和 mp4 文件。</p>
<h3 id="模式难点-3">模式难点</h3>
<h3 id="模式解决问题-4">模式解决问题</h3>
<p>主要解决在软件系统中，常常要将一些&quot;现存的对象&quot;放到新的环境中，而新环境要求的接口是现对象不能满足的。</p>
<h3 id="优点-4">优点</h3>
<ol>
<li>可以让任何两个没有关联的类一起运行。</li>
<li>提高了类的复用。</li>
<li>增加了类的透明度。</li>
<li>灵活性好。</li>
</ol>
<h3 id="缺点-4">缺点</h3>
<ol>
<li>过多地使用适配器，会让系统非常零乱，不易整体进行把握。比如，明明看到调用的是 A 接口，其实内部被适配成了 B 接口的实现，一个系统如果太多出现这种情况，无异于一场灾难。因此如果不是很有必要，可以不使用适配器，而是直接对系统进行重构。 2.由于 JAVA 至多继承一个类，所以至多只能适配一个适配者类，而且目标类必须是抽象类。</li>
</ol>
<h3 id="模式应用场景-4">模式应用场景</h3>
<p>有动机地修改一个正常运行的系统的接口，这时应该考虑使用适配器模式。</p>
<h3 id="模式代码-4">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span><span class="lnt">78
</span><span class="lnt">79
</span><span class="lnt">80
</span><span class="lnt">81
</span><span class="lnt">82
</span><span class="lnt">83
</span><span class="lnt">84
</span><span class="lnt">85
</span><span class="lnt">86
</span><span class="lnt">87
</span><span class="lnt">88
</span><span class="lnt">89
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="k">import</span> <span class="s1">&#39;run.dart&#39;</span><span class="p">;</span>

<span class="c1">//官方媒体播放接口
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">MediaPlayer</span> <span class="p">{</span>
  <span class="kt">void</span> <span class="n">play</span><span class="p">(</span><span class="kt">String</span> <span class="n">audioType</span><span class="p">,</span> <span class="kt">String</span> <span class="n">fileName</span><span class="p">);</span>
<span class="p">}</span>

<span class="c1">//自定义媒体播放接口
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">AdvancedMediaPlayer</span> <span class="p">{</span>
  <span class="kt">void</span> <span class="n">playVlc</span><span class="p">(</span><span class="kt">String</span> <span class="n">fileName</span><span class="p">);</span>
  <span class="kt">void</span> <span class="n">playMp4</span><span class="p">(</span><span class="kt">String</span> <span class="n">fileName</span><span class="p">);</span>
<span class="p">}</span>

<span class="c1">//创建实现了 AdvancedMediaPlayer 接口的实体类。
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">VlcPlayer</span> <span class="kd">implements</span> <span class="n">AdvancedMediaPlayer</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">playMp4</span><span class="p">(</span><span class="kt">String</span> <span class="n">fileName</span><span class="p">)</span> <span class="p">{}</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">playVlc</span><span class="p">(</span><span class="kt">String</span> <span class="n">fileName</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Playing vlc file. Name: &#34;</span> <span class="o">+</span> <span class="n">fileName</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">Mp4Player</span> <span class="kd">implements</span> <span class="n">AdvancedMediaPlayer</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">playVlc</span><span class="p">(</span><span class="kt">String</span> <span class="n">fileName</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">//什么也不做
</span><span class="c1"></span>  <span class="p">}</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">playMp4</span><span class="p">(</span><span class="kt">String</span> <span class="n">fileName</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Playing mp4 file. Name: &#34;</span> <span class="o">+</span> <span class="n">fileName</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="c1">// 创建实现了 MediaPlayer 接口的适配器类。
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">MediaAdapter</span> <span class="kd">implements</span> <span class="n">MediaPlayer</span> <span class="p">{</span>
  <span class="n">AdvancedMediaPlayer</span> <span class="n">advancedMusicPlayer</span><span class="p">;</span>
  <span class="n">MediaAdapter</span><span class="p">(</span><span class="kt">String</span> <span class="n">audioType</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">audioType</span> <span class="o">==</span> <span class="s2">&#34;vlc&#34;</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">advancedMusicPlayer</span> <span class="o">=</span> <span class="k">new</span> <span class="n">VlcPlayer</span><span class="p">();</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">audioType</span> <span class="o">==</span> <span class="s2">&#34;mp4&#34;</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">advancedMusicPlayer</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Mp4Player</span><span class="p">();</span>
    <span class="p">}</span>
  <span class="p">}</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">play</span><span class="p">(</span><span class="kt">String</span> <span class="n">audioType</span><span class="p">,</span> <span class="kt">String</span> <span class="n">fileName</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">audioType</span> <span class="o">==</span> <span class="s2">&#34;vlc&#34;</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">advancedMusicPlayer</span><span class="p">.</span><span class="n">playVlc</span><span class="p">(</span><span class="n">fileName</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">audioType</span> <span class="o">==</span> <span class="s2">&#34;mp4&#34;</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">advancedMusicPlayer</span><span class="p">.</span><span class="n">playMp4</span><span class="p">(</span><span class="n">fileName</span><span class="p">);</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="c1">// 创建实现了 MediaPlayer 接口的实体类。
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">AudioPlayer</span> <span class="kd">implements</span> <span class="n">MediaPlayer</span> <span class="p">{</span>
  <span class="n">MediaAdapter</span> <span class="n">mediaAdapter</span><span class="p">;</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">play</span><span class="p">(</span><span class="kt">String</span> <span class="n">audioType</span><span class="p">,</span> <span class="kt">String</span> <span class="n">fileName</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">//播放 mp3 音乐文件的内置支持
</span><span class="c1"></span>    <span class="k">if</span> <span class="p">(</span><span class="n">audioType</span> <span class="o">==</span> <span class="s2">&#34;mp3&#34;</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Playing mp3 file. Name: &#34;</span> <span class="o">+</span> <span class="n">fileName</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="c1">//mediaAdapter 提供了播放其他文件格式的支持
</span><span class="c1"></span>    <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">audioType</span> <span class="o">==</span> <span class="s2">&#34;vlc&#34;</span> <span class="o">||</span> <span class="n">audioType</span> <span class="o">==</span> <span class="s2">&#34;mp4&#34;</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">mediaAdapter</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MediaAdapter</span><span class="p">(</span><span class="n">audioType</span><span class="p">);</span>
      <span class="n">mediaAdapter</span><span class="p">.</span><span class="n">play</span><span class="p">(</span><span class="n">audioType</span><span class="p">,</span> <span class="n">fileName</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
      <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Invalid media. &#34;</span> <span class="o">+</span> <span class="n">audioType</span> <span class="o">+</span> <span class="s2">&#34; format not supported&#34;</span><span class="p">);</span>
    <span class="p">}</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">RunAdapter</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">AudioPlayer</span> <span class="n">audioPlayer</span> <span class="o">=</span> <span class="k">new</span> <span class="n">AudioPlayer</span><span class="p">();</span>
    <span class="n">audioPlayer</span><span class="p">.</span><span class="n">play</span><span class="p">(</span><span class="s2">&#34;mp3&#34;</span><span class="p">,</span> <span class="s2">&#34;beyond the horizon.mp3&#34;</span><span class="p">);</span>
    <span class="n">audioPlayer</span><span class="p">.</span><span class="n">play</span><span class="p">(</span><span class="s2">&#34;mp4&#34;</span><span class="p">,</span> <span class="s2">&#34;alone.mp4&#34;</span><span class="p">);</span>
    <span class="n">audioPlayer</span><span class="p">.</span><span class="n">play</span><span class="p">(</span><span class="s2">&#34;vlc&#34;</span><span class="p">,</span> <span class="s2">&#34;far far away.vlc&#34;</span><span class="p">);</span>
    <span class="n">audioPlayer</span><span class="p">.</span><span class="n">play</span><span class="p">(</span><span class="s2">&#34;avi&#34;</span><span class="p">,</span> <span class="s2">&#34;mind me.avi&#34;</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;适配器模式&#34;</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="桥接模式bridge">桥接模式（Bridge)</h2>
<h3 id="模式分析-5">模式分析</h3>
<p>桥接（Bridge）是用于把抽象化与实现化解耦，使得二者可以独立变化。这种类型的设计模式属于结构型模式，它通过提供抽象化和实现化之间的桥接结构，来实现二者的解耦。</p>
<p>这种模式涉及到一个作为桥接的接口，使得实体类的功能独立于接口实现类。这两种类型的类可被结构化改变而互不影响。</p>
<p>我们通过下面的实例来演示桥接模式（Bridge Pattern）的用法。其中，可以使用相同的抽象类方法但是不同的桥接实现类，来画出不同颜色的圆。</p>
<h3 id="模式难点-4">模式难点</h3>
<h3 id="模式解决问题-5">模式解决问题</h3>
<p>在有多种可能会变化的情况下，用继承会造成类爆炸问题，扩展起来不灵活。</p>
<h3 id="优点-5">优点</h3>
<ol>
<li>抽象和实现的分离。</li>
<li>优秀的扩展能力。</li>
<li>实现细节对客户透明。</li>
</ol>
<h3 id="缺点-5">缺点</h3>
<p>桥接模式的引入会增加系统的理解与设计难度，由于聚合关联关系建立在抽象层，要求开发者针对抽象进行设计与编程。</p>
<h3 id="模式应用场景-5">模式应用场景</h3>
<ol>
<li>如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性，避免在两个层次之间建立静态的继承联系，通过桥接模式可以使它们在抽象层建立一个关联关系。</li>
<li>对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统，桥接模式尤为适用。</li>
<li>一个类存在两个独立变化的维度，且这两个维度都需要进行扩展。</li>
</ol>
<h3 id="模式代码-5">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="c1">//咖啡售卖系统中，容量作为抽象化（Abstraction），口味作为实现化（Implementor）
</span><span class="c1">//创建口味接口
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">ICoffeeAdditives</span> <span class="p">{</span>
  <span class="kt">void</span> <span class="n">addSomething</span><span class="p">();</span>
<span class="p">}</span>

<span class="c1">// 创建抽象化类
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Coffee</span> <span class="p">{</span>
  <span class="n">ICoffeeAdditives</span> <span class="n">additives</span><span class="p">;</span>
  <span class="n">Coffee</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="n">additives</span><span class="p">);</span>
  <span class="kt">void</span> <span class="n">orderCoffee</span><span class="p">(</span><span class="kt">int</span> <span class="n">count</span><span class="p">);</span>
<span class="p">}</span>

<span class="c1">//修正抽象化类，增加品控方法
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">RefinedCoffee</span> <span class="kd">extends</span> <span class="n">Coffee</span> <span class="p">{</span>
  <span class="n">RefinedCoffee</span><span class="p">(</span><span class="n">ICoffeeAdditives</span> <span class="n">additives</span><span class="p">)</span> <span class="o">:</span> <span class="k">super</span><span class="p">(</span><span class="n">additives</span><span class="p">);</span>
  <span class="n">checkQuality</span><span class="p">(</span><span class="kt">int</span> <span class="n">score</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;添加&#34;</span> <span class="o">+</span> <span class="n">score</span><span class="p">.</span><span class="n">toString</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&#34;%&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="c1">//创建实现化部分
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">Milk</span> <span class="kd">implements</span> <span class="n">ICoffeeAdditives</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">addSomething</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;加奶&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">Sugar</span> <span class="kd">implements</span> <span class="n">ICoffeeAdditives</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">addSomething</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;加糖&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">LargeCoffee</span> <span class="kd">extends</span> <span class="n">RefinedCoffee</span> <span class="p">{</span>
  <span class="n">LargeCoffee</span><span class="p">(</span><span class="n">ICoffeeAdditives</span> <span class="n">additives</span><span class="p">)</span> <span class="o">:</span> <span class="k">super</span><span class="p">(</span><span class="n">additives</span><span class="p">);</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">orderCoffee</span><span class="p">(</span><span class="kt">int</span> <span class="n">count</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">additives</span><span class="p">.</span><span class="n">addSomething</span><span class="p">();</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;大杯咖啡&#34;</span> <span class="o">+</span> <span class="n">count</span><span class="p">.</span><span class="n">toString</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&#34;杯&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>
<span class="kd">class</span> <span class="nc">MidCoffee</span> <span class="kd">extends</span> <span class="n">RefinedCoffee</span> <span class="p">{</span>
  <span class="n">MidCoffee</span><span class="p">(</span><span class="n">ICoffeeAdditives</span> <span class="n">additives</span><span class="p">)</span> <span class="o">:</span> <span class="k">super</span><span class="p">(</span><span class="n">additives</span><span class="p">);</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">orderCoffee</span><span class="p">(</span><span class="kt">int</span> <span class="n">count</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">additives</span><span class="p">.</span><span class="n">addSomething</span><span class="p">();</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;中杯咖啡&#34;</span> <span class="o">+</span> <span class="n">count</span><span class="p">.</span><span class="n">toString</span><span class="p">()</span> <span class="o">+</span> <span class="s2">&#34;杯&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>
<span class="kd">class</span> <span class="nc">RunBridge</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;桥接模式&#34;</span><span class="p">;</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">RefinedCoffee</span> <span class="n">largeWithMilk</span> <span class="o">=</span> <span class="k">new</span> <span class="n">LargeCoffee</span><span class="p">(</span><span class="n">Milk</span><span class="p">());</span>
    <span class="n">largeWithMilk</span><span class="p">.</span><span class="n">orderCoffee</span><span class="p">(</span><span class="m">2</span><span class="p">);</span>
    <span class="n">largeWithMilk</span><span class="p">.</span><span class="n">checkQuality</span><span class="p">(</span><span class="m">90</span><span class="p">);</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n</span><span class="s2">&#34;</span><span class="p">);</span>
    <span class="n">RefinedCoffee</span> <span class="n">largeWithSugar</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MidCoffee</span><span class="p">(</span><span class="n">Sugar</span><span class="p">());</span>
    <span class="n">largeWithSugar</span><span class="p">.</span><span class="n">orderCoffee</span><span class="p">(</span><span class="m">1</span><span class="p">);</span>
    <span class="n">largeWithSugar</span><span class="p">.</span><span class="n">checkQuality</span><span class="p">(</span><span class="m">100</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="装饰器模式decorator-pattern">装饰器模式（Decorator Pattern）</h2>
<h3 id="模式分析-6">模式分析</h3>
<p>装饰器模式（Decorator Pattern）允许向一个现有的对象添加新的功能，同时又不改变其结构。这种类型的设计模式属于结构型模式，它是作为现有的类的一个包装。</p>
<p>这种模式创建了一个装饰类，用来包装原有的类，并在保持类方法签名完整性的前提下，提供了额外的功能。</p>
<p>我们通过下面的实例来演示装饰器模式的用法。其中，我们将把一个形状装饰上不同的颜色，同时又不改变形状类。</p>
<h3 id="模式难点-5">模式难点</h3>
<p>实现现有抽象对象并传入已实现的抽象对象，并加入自定义方法</p>
<h3 id="模式解决问题-6">模式解决问题</h3>
<p>一般的，我们为了扩展一个类经常使用继承方式实现，由于继承为类引入静态特征，并且随着扩展功能的增多，子类会很膨胀。</p>
<h3 id="优点-6">优点</h3>
<p>装饰类和被装饰类可以独立发展，不会相互耦合，装饰模式是继承的一个替代模式，装饰模式可以动态扩展一个实现类的功能。</p>
<h3 id="缺点-6">缺点</h3>
<p>多层装饰比较复杂。</p>
<h3 id="模式应用场景-6">模式应用场景</h3>
<ol>
<li>扩展一个类的功能。</li>
<li>动态增加功能，动态撤销。</li>
</ol>
<h3 id="模式代码-6">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="c1">//创建接口
</span><span class="c1"></span><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Shape</span> <span class="p">{</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">();</span>
<span class="p">}</span>

<span class="c1">//创建接口实体类
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">Rectangle</span> <span class="kd">implements</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;shape: Rectangle&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">Circle</span> <span class="kd">implements</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;shape: Circle&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="c1">//创建实现了Shape接口的抽象类
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">ShapeDecorator</span> <span class="kd">implements</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="kd">final</span> <span class="n">Shape</span> <span class="n">decoratedShape</span><span class="p">;</span>
  <span class="n">ShapeDecorator</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="n">decoratedShape</span><span class="p">);</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">decoratedShape</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="c1">//创建扩展了 ShapeDecorator 类的实体装饰类。
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">RedShapeDecorator</span> <span class="kd">extends</span> <span class="n">ShapeDecorator</span> <span class="p">{</span>
  <span class="n">RedShapeDecorator</span><span class="p">(</span><span class="n">Shape</span> <span class="n">decoratedShape</span><span class="p">)</span> <span class="o">:</span> <span class="k">super</span><span class="p">(</span><span class="n">decoratedShape</span><span class="p">);</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">decoratedShape</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>
    <span class="n">_setRedBorder</span><span class="p">(</span><span class="n">decoratedShape</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="kt">void</span> <span class="n">_setRedBorder</span><span class="p">(</span><span class="n">Shape</span> <span class="n">decoratedShape</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Border Color: Red&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>
<span class="c1">//使用 RedShapeDecorator 来装饰 Shape 对象。
</span><span class="c1"></span>
<span class="kd">class</span> <span class="nc">RunDecorator</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">Shape</span> <span class="n">circle</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Circle</span><span class="p">();</span>
    <span class="c1">// ShapeDecorator redCircle = new RedShapeDecorator(new Circle());
</span><span class="c1"></span>    <span class="c1">// ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangle());
</span><span class="c1"></span>    <span class="n">Shape</span> <span class="n">redCircle</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RedShapeDecorator</span><span class="p">(</span><span class="k">new</span> <span class="n">Circle</span><span class="p">());</span>
    <span class="n">Shape</span> <span class="n">redRectangle</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RedShapeDecorator</span><span class="p">(</span><span class="k">new</span> <span class="n">Rectangle</span><span class="p">());</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Circle with normal border&#34;</span><span class="p">);</span>
    <span class="n">circle</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>

    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n</span><span class="s2">Circle of red border&#34;</span><span class="p">);</span>
    <span class="n">redCircle</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>

    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n</span><span class="s2">Rectangle of red border&#34;</span><span class="p">);</span>
    <span class="n">redRectangle</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>
  <span class="p">}</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;装饰器模式&#34;</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="外观模式facade">外观模式(Facade)</h2>
<h3 id="模式分析-7">模式分析</h3>
<p>外观模式（Facade Pattern）隐藏系统的复杂性，并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式，它向现有的系统添加一个接口，来隐藏系统的复杂性。</p>
<p>这种模式涉及到一个单一的类，该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。</p>
<h3 id="模式难点-6">模式难点</h3>
<h3 id="模式解决问题-7">模式解决问题</h3>
<p>降低访问复杂系统的内部子系统时的复杂度，简化客户端与之的接口。</p>
<h3 id="优点-7">优点</h3>
<ol>
<li>减少系统相互依赖。</li>
<li>提高灵活性。</li>
<li>提高了安全性。</li>
</ol>
<h3 id="缺点-7">缺点</h3>
<p>不符合开闭原则，如果要改东西很麻烦，继承重写都不合适。</p>
<h3 id="模式应用场景-7">模式应用场景</h3>
<ol>
<li>为复杂的模块或子系统提供外界访问的模块。</li>
<li>子系统相对独立。</li>
<li>预防低水平人员带来的风险。</li>
</ol>
<h3 id="模式代码-7">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Shape</span> <span class="p">{</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">();</span>
<span class="p">}</span>

<span class="c1">// 创建实现接口的实体类。
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">Rectangle</span> <span class="kd">implements</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Rectangle::draw()&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">Square</span> <span class="kd">implements</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Square::draw()&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">Circle</span> <span class="kd">implements</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Circle::draw()&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="c1">// 创建一个外观类。
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">ShapeMaker</span> <span class="p">{</span>
  <span class="n">Shape</span> <span class="n">circle</span><span class="p">;</span>
  <span class="n">Shape</span> <span class="n">rectangle</span><span class="p">;</span>
  <span class="n">Shape</span> <span class="n">square</span><span class="p">;</span>

  <span class="n">ShapeMaker</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">circle</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Circle</span><span class="p">();</span>
    <span class="n">rectangle</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Rectangle</span><span class="p">();</span>
    <span class="n">square</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Square</span><span class="p">();</span>
  <span class="p">}</span>

  <span class="kt">void</span> <span class="n">drawCircle</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">circle</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>
  <span class="p">}</span>

  <span class="kt">void</span> <span class="n">drawRectangle</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">rectangle</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>
  <span class="p">}</span>

  <span class="kt">void</span> <span class="n">drawSquare</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">square</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">RunFacade</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">ShapeMaker</span> <span class="n">shapeMaker</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ShapeMaker</span><span class="p">();</span>
    <span class="n">shapeMaker</span><span class="p">.</span><span class="n">drawCircle</span><span class="p">();</span>
    <span class="n">shapeMaker</span><span class="p">.</span><span class="n">drawRectangle</span><span class="p">();</span>
    <span class="n">shapeMaker</span><span class="p">.</span><span class="n">drawSquare</span><span class="p">();</span>
  <span class="p">}</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;外观模式&#34;</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="享元模式flyweight">享元模式(Flyweight)</h2>
<h3 id="模式分析-8">模式分析</h3>
<p>享元模式（Flyweight Pattern）主要用于减少创建对象的数量，以减少内存占用和提高性能。这种类型的设计模式属于结构型模式，它提供了减少对象数量从而改善应用所需的对象结构的方式。</p>
<p>享元模式尝试重用现有的同类对象，如果未找到匹配的对象，则创建新对象。我们将通过创建 5 个对象来画出 20 个分布于不同位置的圆来演示这种模式。由于只有 5 种可用的颜色，所以 color 属性被用来检查现有的 Circle 对象。</p>
<h3 id="模式难点-7">模式难点</h3>
<p>使用Map讲重复创建的对象进行存储，需要严格分离出外部状态和内部状态</p>
<h3 id="模式解决问题-8">模式解决问题</h3>
<p>在有大量对象时，有可能会造成内存溢出，我们把其中共同的部分抽象出来，如果有相同的业务请求，直接返回在内存中已有的对象，避免重新创建。</p>
<h3 id="优点-8">优点</h3>
<p>大大减少对象的创建，降低系统的内存，使效率提高。</p>
<h3 id="缺点-8">缺点</h3>
<p>提高了系统的复杂度，需要分离出外部状态和内部状态，而且外部状态具有固有化的性质，不应该随着内部状态的变化而变化，否则会造成系统的混乱。</p>
<h3 id="模式应用场景-8">模式应用场景</h3>
<ol>
<li>系统有大量相似对象。</li>
<li>需要缓冲池的场景。</li>
</ol>
<h3 id="模式代码-8">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span><span class="lnt">78
</span><span class="lnt">79
</span><span class="lnt">80
</span><span class="lnt">81
</span><span class="lnt">82
</span><span class="lnt">83
</span><span class="lnt">84
</span><span class="lnt">85
</span><span class="lnt">86
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="k">import</span> <span class="s1">&#39;dart:math&#39;</span> <span class="k">as</span> <span class="n">math</span><span class="p">;</span>

<span class="k">import</span> <span class="s1">&#39;run.dart&#39;</span><span class="p">;</span>

<span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Shape</span> <span class="p">{</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">();</span>
<span class="p">}</span>

<span class="c1">// 创建实现接口的实体类。
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">Circle</span> <span class="kd">implements</span> <span class="n">Shape</span> <span class="p">{</span>
  <span class="kt">String</span> <span class="n">color</span><span class="p">;</span>
  <span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
  <span class="kt">int</span> <span class="n">y</span><span class="p">;</span>
  <span class="kt">int</span> <span class="n">radius</span><span class="p">;</span>
  <span class="n">Circle</span><span class="p">(</span><span class="kt">String</span> <span class="n">color</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="n">color</span> <span class="o">=</span> <span class="n">color</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="kt">void</span> <span class="n">setX</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="kt">void</span> <span class="n">setY</span><span class="p">(</span><span class="kt">int</span> <span class="n">y</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="kt">void</span> <span class="n">setRadius</span><span class="p">(</span><span class="kt">int</span> <span class="n">radius</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="n">radius</span> <span class="o">=</span> <span class="n">radius</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">draw</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Circle: Draw() [Color : &#34;</span> <span class="o">+</span>
        <span class="n">color</span> <span class="o">+</span>
        <span class="s2">&#34;, x : &#34;</span> <span class="o">+</span>
        <span class="n">x</span><span class="p">.</span><span class="n">toString</span><span class="p">()</span> <span class="o">+</span>
        <span class="s2">&#34;, y :&#34;</span> <span class="o">+</span>
        <span class="n">y</span><span class="p">.</span><span class="n">toString</span><span class="p">()</span> <span class="o">+</span>
        <span class="s2">&#34;, radius :&#34;</span> <span class="o">+</span>
        <span class="n">radius</span><span class="p">.</span><span class="n">toString</span><span class="p">());</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="c1">// 创建一个工厂，生成基于给定信息的实体类的对象。
</span><span class="c1"></span><span class="kd">class</span> <span class="nc">ShapeFactory</span> <span class="p">{</span>
  <span class="kd">static</span> <span class="kd">final</span> <span class="n">Map</span><span class="o">&lt;</span><span class="kt">String</span><span class="p">,</span> <span class="n">Shape</span><span class="o">&gt;</span> <span class="n">circleMap</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Map</span><span class="p">();</span>

  <span class="kd">static</span> <span class="n">Shape</span> <span class="n">getCircle</span><span class="p">(</span><span class="kt">String</span> <span class="n">color</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">Circle</span> <span class="n">circle</span> <span class="o">=</span> <span class="n">circleMap</span><span class="p">[</span><span class="n">color</span><span class="p">];</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">circle</span> <span class="o">==</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">circle</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Circle</span><span class="p">(</span><span class="n">color</span><span class="p">);</span>
      <span class="n">circleMap</span><span class="p">[</span><span class="n">color</span><span class="p">]</span> <span class="o">=</span> <span class="n">circle</span><span class="p">;</span>
      <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Creating circle of color : &#34;</span> <span class="o">+</span> <span class="n">color</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">circle</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">RunFlyweight</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="kd">final</span> <span class="n">List</span><span class="o">&lt;</span><span class="kt">String</span><span class="o">&gt;</span> <span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&#34;Red&#34;</span><span class="p">,</span> <span class="s2">&#34;Green&#34;</span><span class="p">,</span> <span class="s2">&#34;Blue&#34;</span><span class="p">,</span> <span class="s2">&#34;White&#34;</span><span class="p">,</span> <span class="s2">&#34;Black&#34;</span><span class="p">];</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="m">20</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
      <span class="n">Circle</span> <span class="n">circle</span> <span class="o">=</span> <span class="n">ShapeFactory</span><span class="p">.</span><span class="n">getCircle</span><span class="p">(</span><span class="n">getRandomColor</span><span class="p">());</span>
      <span class="n">circle</span><span class="p">.</span><span class="n">setX</span><span class="p">(</span><span class="n">getRandomX</span><span class="p">());</span>
      <span class="n">circle</span><span class="p">.</span><span class="n">setY</span><span class="p">(</span><span class="n">getRandomY</span><span class="p">());</span>
      <span class="n">circle</span><span class="p">.</span><span class="n">setRadius</span><span class="p">(</span><span class="m">100</span><span class="p">);</span>
      <span class="n">circle</span><span class="p">.</span><span class="n">draw</span><span class="p">();</span>
    <span class="p">}</span>
  <span class="p">}</span>

  <span class="kt">String</span> <span class="n">getRandomColor</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">colors</span><span class="p">[</span><span class="n">math</span><span class="p">.</span><span class="n">Random</span><span class="p">().</span><span class="n">nextInt</span><span class="p">(</span><span class="n">colors</span><span class="p">.</span><span class="n">length</span><span class="p">)];</span>
  <span class="p">}</span>

  <span class="kt">int</span> <span class="n">getRandomX</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">math</span><span class="p">.</span><span class="n">Random</span><span class="p">().</span><span class="n">nextInt</span><span class="p">(</span><span class="m">100</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="kt">int</span> <span class="n">getRandomY</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">return</span> <span class="n">math</span><span class="p">.</span><span class="n">Random</span><span class="p">().</span><span class="n">nextInt</span><span class="p">(</span><span class="m">100</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;享元模式&#34;</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="代理模式">代理模式</h2>
<h3 id="模式分析-9">模式分析</h3>
<p>在代理模式（Proxy Pattern）中，一个类代表另一个类的功能。这种类型的设计模式属于结构型模式。</p>
<p>在代理模式中，我们创建具有现有对象的对象，以便向外界提供功能接口。</p>
<h3 id="模式难点-8">模式难点</h3>
<h3 id="模式解决问题-9">模式解决问题</h3>
<p>在直接访问对象时带来的问题，比如说：要访问的对象在远程的机器上。在面向对象系统中，有些对象由于某些原因（比如对象创建开销很大，或者某些操作需要安全控制，或者需要进程外的访问），直接访问会给使用者或者系统结构带来很多麻烦，我们可以在访问此对象时加上一个对此对象的访问层。</p>
<h3 id="优点-9">优点</h3>
<ol>
<li>你可以在客户端毫无察觉的情况下控制服务对象。</li>
<li>如果客户端对服务对象的生命周期没有特殊要求， 你可以对生命周期进行管理。</li>
<li>即使服务对象还未准备好或不存在， 代理也可以正常工作。</li>
<li>开闭原则。 你可以在不对服务或客户端做出修改的情况下创建新代理。</li>
</ol>
<h3 id="缺点-9">缺点</h3>
<ol>
<li>由于在客户端和真实主题之间增加了代理对象，因此有些类型的代理模式可能会造成请求的处理速度变慢。</li>
<li>实现代理模式需要额外的工作，有些代理模式的实现非常复杂。</li>
</ol>
<h3 id="模式应用场景-9">模式应用场景</h3>
<p>按职责来划分，通常有以下使用场景：</p>
<ol>
<li>本地执行远程服务 （远程代理）。 适用于服务对象位于远程服务器上的情形。</li>
<li>延迟初始化 （虚拟代理）。 如果你有一个偶尔使用的重量级服务对象， 一直保持该对象运行会消耗系统资源时， 可使用代理模式。</li>
<li>Copy-on-Write 代理。</li>
<li>访问控制 （保护代理）。 如果你只希望特定客户端使用服务对象， 这里的对象可以是操作系统中非常重要的部分， 而客户端则是各种已启动的程序 （包括恶意程序）， 此时可使用代理模式。</li>
<li>Cache代理。</li>
<li>防火墙（Firewall）代理。</li>
<li>同步化（Synchronization）代理。</li>
<li>智能引用（Smart Reference）代理。 可在没有客户端使用某个重量级对象时立即销毁该对象。</li>
<li>记录日志请求 （日志记录代理）。 适用于当你需要保存对于服务对象的请求历史记录时。 代理可以在向服务传递请求前进行记录。</li>
</ol>
<h3 id="模式代码-9">模式代码</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-dart" data-lang="dart"><span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">Subject</span> <span class="p">{</span>
  <span class="kt">void</span> <span class="n">request</span><span class="p">();</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">RealSubject</span> <span class="kd">implements</span> <span class="n">Subject</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="n">RealSubject</span><span class="p">();</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">request</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;RealSubject:正在连接&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">SubjectProxy</span> <span class="kd">implements</span> <span class="n">Subject</span> <span class="p">{</span>
  <span class="kd">final</span> <span class="n">RealSubject</span> <span class="n">_realSubject</span><span class="p">;</span>
  <span class="kd">const</span> <span class="n">SubjectProxy</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="n">_realSubject</span><span class="p">);</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">request</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="n">_checkAccess</span><span class="p">())</span> <span class="p">{</span>
      <span class="k">this</span><span class="p">.</span><span class="n">_realSubject</span><span class="p">.</span><span class="n">request</span><span class="p">();</span>
      <span class="k">this</span><span class="p">.</span><span class="n">_logAccess</span><span class="p">();</span>
    <span class="p">}</span>
  <span class="p">}</span>

  <span class="kt">bool</span> <span class="n">_checkAccess</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Proxy: 判断当前是否可连接&#34;</span><span class="p">);</span>
    <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
  <span class="p">}</span>

  <span class="kt">void</span> <span class="n">_logAccess</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s2">&#34;Proxy: 写一些日志比如连接时间&#34;</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">}</span>

<span class="kd">class</span> <span class="nc">RunProxy</span> <span class="kd">implements</span> <span class="n">Run</span> <span class="p">{</span>
  <span class="err">@</span><span class="n">override</span>
  <span class="kt">void</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">print</span><span class="p">(</span><span class="s1">&#39;Client: 使用RealSubject类：&#39;</span><span class="p">);</span>
    <span class="kd">const</span> <span class="n">realSubject</span> <span class="o">=</span> <span class="kd">const</span> <span class="n">RealSubject</span><span class="p">();</span>
    <span class="n">_clientCode</span><span class="p">(</span><span class="n">realSubject</span><span class="p">);</span>
    <span class="n">print</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">);</span>
    <span class="n">print</span><span class="p">(</span><span class="s1">&#39;Client: 使用代理类：&#39;</span><span class="p">);</span>
    <span class="kd">const</span> <span class="n">proxy</span> <span class="o">=</span> <span class="kd">const</span> <span class="n">SubjectProxy</span><span class="p">(</span><span class="n">realSubject</span><span class="p">);</span>
    <span class="n">_clientCode</span><span class="p">(</span><span class="n">proxy</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="n">_clientCode</span><span class="p">(</span><span class="n">Subject</span> <span class="n">subject</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// ...
</span><span class="c1"></span>    <span class="n">print</span><span class="p">(</span><span class="s1">&#39;做一些连接前的事情&#39;</span><span class="p">);</span>
    <span class="n">subject</span><span class="p">.</span><span class="n">request</span><span class="p">();</span>

    <span class="c1">// ...
</span><span class="c1"></span>  <span class="p">}</span>

  <span class="err">@</span><span class="n">override</span>
  <span class="kt">String</span> <span class="n">name</span> <span class="o">=</span> <span class="s2">&#34;代理模式&#34;</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></td></tr></table>
</div>
</div><h2 id="发布订阅模式">发布订阅模式</h2>
<h3 id="模式分析-10">模式分析</h3>
<h3 id="模式难点-9">模式难点</h3>
<h3 id="模式解决问题-10">模式解决问题</h3>
<h3 id="优点-10">优点</h3>
<h3 id="缺点-10">缺点</h3>
<h3 id="模式应用场景-10">模式应用场景</h3>
<h3 id="模式代码-10">模式代码</h3></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2021-07-28</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" data-title="《Dart语言基础之设计模式》" data-hashtags="flutter"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" data-hashtag="flutter"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Hacker News" data-sharer="hackernews" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" data-title="《Dart语言基础之设计模式》"><i class="fab fa-hacker-news fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" data-title="《Dart语言基础之设计模式》"><i data-svg-src="https://cdn.jsdelivr.net/npm/simple-icons@2.14.0/icons/line.svg"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://luckly.work/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" data-title="《Dart语言基础之设计模式》" data-image="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"><i class="fab fa-weibo fa-fw"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/flutter/">flutter</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/dart%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80%E4%B9%8Bstring/" class="prev" rel="prev" title="《Dart语言基础之String》"><i class="fas fa-angle-left fa-fw"></i>《Dart语言基础之String》</a>
            <a href="/flutter%E4%B8%ADshapeborder%E4%BD%BF%E7%94%A8%E6%80%BB%E7%BB%93/" class="next" rel="next" title="《Flutter中ShapeBorder使用总结》">《Flutter中ShapeBorder使用总结》<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.79.1">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">早起的年轻人</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cookieconsent@3.1.1/build/cookieconsent.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/smooth-scroll@16.1.3/dist/smooth-scroll.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.37.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/algoliasearch@4.2.0/dist/algoliasearch-lite.umd.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.2.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.4.0/sharer.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/cookieconsent@3.1.1/build/cookieconsent.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":10},"comment":{},"cookieconsent":{"content":{"dismiss":"同意","link":"了解更多","message":"本网站使用 Cookies 来改善您的浏览体验."},"enable":true,"palette":{"button":{"background":"#f0f0f0"},"popup":{"background":"#1aa3ff"}},"theme":"edgeless"},"search":{"algoliaAppID":"PASDMWALPK","algoliaIndex":"index.zh-cn","algoliaSearchKey":"b42948e51daaa93df92381c8e2ac0f93","highlightTag":"em","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"algolia"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
